package com.example.dllo.my36kr.my;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.dllo.my36kr.R;
import com.example.dllo.my36kr.message.enter.EnterAty;
import com.example.dllo.my36kr.message.enter.PicAty;
import com.example.dllo.my36kr.my.collect.CollectAty;
import com.example.dllo.my36kr.tools.BaseFragment;
import com.example.dllo.my36kr.tools.CircleDrawable;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;

/**
 * Created by dllo on 16/8/15.
 */

/**
 * 我的 http://www.cnblogs.com/zhwl/p/3332636.html
 */
public class MyFragment extends BaseFragment {

    private MyPopupWindow window;
    private TextView enterTv;
    private LinearLayout frameLayout;
    private ImageView headIv;
    private FlushService service;
    private LinearLayout collectLl;
    private MyUser myUser;


    @Override
    protected int setLayout() {
        return R.layout.fragment_my;

    }

//    @Override
//    public void onResume() {
//        super.onResume();
//        set();
//    }
//
//    public void set(){
//        MyUser myUser = BmobUser.getCurrentUser(MyUser.class);
//        if (myUser != null){
//            if (myUser.getIcon() != null){
//                headIv.setImageBitmap(myUser.getIcon());
//                enterTv.setText(myUser.getUsername());
//            }else {
//                Toast.makeText(mContext, "请上传头像", Toast.LENGTH_SHORT).show();
//            }
//        }else {
//            Toast.makeText(mContext, "重新登入", Toast.LENGTH_SHORT).show();
//
//        }
//    }
    @Override
    protected void initView() {
        LinearLayout layout = bindView(R.id.ll_my_chouti);
        frameLayout = bindView(R.id.fl_my_dengru);
        //收藏
        collectLl = bindView(R.id.ll_my_collect);

        headIv = bindView(R.id.iv_my_touxiang);
        headIv.setOnClickListener(new View.OnClickListener() {//显示用户名
            @Override
            public void onClick(View v) {
                if (myUser ==  null){
                    startActivity(new Intent(getActivity(), EnterAty.class));
                }else {
                    startActivity(new Intent(getActivity(), PicAty.class));
                }
            }
        });
        enterTv = bindView(R.id.tv_my_dengru);
        window = new MyPopupWindow(getActivity(), itemsOnClick);
        layout.setOnClickListener(new View.OnClickListener() {//电话
            @Override
            public void onClick(View v) {
                window.showAtLocation(getActivity().findViewById(R.id.ll_my_chouti), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
            }
        });
    }
    @Override
    protected void initData() {
        myUser = BmobUser.getCurrentUser(MyUser.class);
        final BmobUser bmobUser = BmobUser.getCurrentUser();
        if (bmobUser != null){
            enterTv.setText(bmobUser.getUsername());
            flush();

        }else {
            frameLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(getActivity(), EnterAty.class));
                }
            });
        }

        collectLl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (myUser !=  null){
                    startActivity(new Intent(getActivity(), CollectAty.class));
                }else {
                    startActivity(new Intent(getActivity(), EnterAty.class));
                }
            }
        });

    }



    public void flush(){//刷新
        MyUser myUser = new MyUser();
        BmobQuery<MyUser> query = new BmobQuery<>();
        myUser = BmobUser.getCurrentUser(MyUser.class);
        query.getObject(myUser.getObjectId(), new QueryListener<MyUser>() {
            @Override
            public void done(MyUser myUser, BmobException e) {
                if (e == null){
                    Bitmap bitmap = myUser.getIcon();
                    if (bitmap != null){
                        CircleDrawable circleDrawable = new CircleDrawable(bitmap);
                        headIv.setImageDrawable(circleDrawable);
                    }
                }else {
                    Toast.makeText(getContext(), "图片显示失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private View.OnClickListener itemsOnClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            window.dismiss();
        }
    };

    @Override
    public void onStart() {
        super.onStart();
        service = new FlushService();
        IntentFilter filter = new IntentFilter();
        filter.addAction("flush");
        filter.addAction("quit");
        filter.addAction("phone");
        getContext().registerReceiver(service,filter);
    }




    @Override
    public void onDestroyView() {
        super.onDestroyView();
        getContext().unregisterReceiver(service);
    }

    private class FlushService extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            switch (action) {
                case "flush"://刷新头像
                    flush();
                    break;
                case "quit"://退出登入
                    MyUser.logOut();
                    headIv.setImageResource(R.mipmap.ic_launcher);
                    enterTv.setText("未登录");
                    frameLayout.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            startActivity(new Intent(getActivity(), EnterAty.class));
                        }
                    });
                    break;
                case "phone":
                    String phone = intent.getStringExtra("phone");
                    enterTv.setText(phone);
                    if (phone != null){
                        flush();
                        frameLayout.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                return;
                            }
                        });
                    }
                    break;
            }
        }
    }


}
